home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / biz / dbase / Ax.lha / Ax / Rexx / FTP_AmiFTP.ax < prev    next >
Text File  |  1997-03-15  |  9KB  |  352 lines

  1. /*
  2.  * Ax ARexx-FTP interface script
  3.  * FTP client: AmiFTP
  4.  * $VER: FTP_AmiFTP.ax 1.0 (26.6.96)
  5.  *
  6.  * Version 1.0
  7.  * by Paul A. Schifferer
  8.  * Copyright 1996-7 © Isengard Developments
  9.  *
  10.  * This script is freely distributable and modifiable.  You are authorized to
  11.  * modify it to suit your needs.  Isengard Developments makes no warranty,
  12.  * express or implied, for its usability or reliability in its original or
  13.  * modified form.
  14.  *
  15.  * This script takes an optional list of files to retrieve with the FTP client
  16.  * program noted above.  If 'files' aren't specified on the command line, the
  17.  * script will attempt to interface with Ax, using the ARexx port name "AXFTP"
  18.  * to get files.  Alternatively, 'files' can be "LIST=filename", which
  19.  * specifies the list of files to use (see below).  If files are specified on
  20.  * the command line for this script, then the first parameter must be the site
  21.  * name, the second the base directory, and all subsequent parameters are
  22.  * files in the form of 'path/filename' (relative to the base path).
  23.  *
  24.  * The file list is in the following format:
  25.  *
  26.  * The first line is the site from which to retrieve the files.  The second
  27.  * line is the base directory to use, e.g., '/pub/aminet', without the
  28.  * trailing slash.  This dir should always be an absolute path, i.e., with
  29.  * the beginning slash.
  30.  *
  31.  * All subsequent lines are files to retrieve in the form of 'path/filename',
  32.  * e.g., "biz/dbase/Ax.lha".  'path' need not be specified, if the file is
  33.  * located in the base directory.  Note that all paths should be specified
  34.  * relative to the base directory, but this is not a requirement.  Absolute
  35.  * pathnames may be used.
  36.  *
  37.  * A line with three hashes ('###') ends the filelist.  Everything thereafter
  38.  * is ignored.
  39.  */
  40.  
  41. options results
  42. signal on syntax
  43. signal on error
  44.  
  45. parse arg files
  46.  
  47. /*
  48.  * Is Ax running?
  49.  * (Ax must be running if a list of files is not specified, else it has no
  50.  * place to get them...)
  51.  */
  52. if (length(files) = 0) & ~show('P',"AX") then do
  53.   say "Ax must be running to use this script!"
  54.   exit 20
  55.   end
  56.  
  57. /*
  58.  * Set up some variables.
  59.  */
  60. clientname = "AmiFTP"
  61. scriptname = "FTP_"clientname".ax"
  62. clientpath = "AmiFTP"
  63. clientopts = "ICONIFIED"
  64. clientport = "AMIFTP"
  65. ftpport = ""
  66.  
  67. /*
  68.  * Command variables.
  69.  *
  70.  * The following variables are commands that would be issued to the FTP client
  71.  * program.  The construct is simple:  "command parameter options".  Place
  72.  * the client's command portion in the variable <a-cmd>cmd, where <a-cmd> is
  73.  * the type of command.  This script will place after the command any parameters
  74.  * that are necessary.  If the command has options to be specified, place them
  75.  * in the variable <a-cmd>opts.  These will be placed at the end of the command,
  76.  * after the parameters.
  77.  *
  78.  * For example, the "GET" command for DaFTP allows you to specify the transfer
  79.  * mode.  To set up this command, you would put "GET" in the variable 'getcmd',
  80.  * and "BINARY" in the variable 'getopts'.  When the command is issued, it
  81.  * will be sent to DaFTP as "GET <filename> BINARY".
  82.  *
  83.  * It's really very simple.  If you are having problems with it, just send me
  84.  * e-mail <gandalf@hughes.net>, and we'll work it out, okay?
  85.  */
  86. sitecmd = "SITE"
  87. siteopts = ""
  88. connectcmd = "CONNECT"
  89. connectopts = ""
  90. portcmd = "PORT"
  91. portopts = ""
  92. binarycmd = ""
  93. binaryopts = ""
  94. cdcmd = "CD"
  95. cdopts = ""
  96. lcdcmd = "LCD"
  97. lcdopts = ""
  98. getcmd = "GET"
  99. getopts = "BIN"
  100. quitcmd = "QUIT"
  101. quitopts = ""
  102.  
  103. /*
  104.  * Check if environment var for FTPCLIENT is set and use it.  The first line
  105.  * in the var would be the full path of the FTP client to use.  The second
  106.  * line, which is optional, specifies any options to use on the command line
  107.  * when starting the client program.
  108.  */
  109. if exists("ENV:FTPCLIENT") then do
  110.   if open('fc',"ENV:FTPCLIENT",'R') then do
  111.     line = strip(readln('fc'))
  112.     if length(line) > 0 then clientpath = line
  113.     line = strip(readln('fc'))
  114.     if length(line) > 0 then clientopts = line
  115.     end
  116.   call close('fc')
  117.   end
  118.  
  119. /*
  120.  * Start FTP client.
  121.  */
  122. if ~show('P',clientport) then do
  123.   say "Starting FTP client..."
  124.   address command "Run >NIL: "clientpath clientopts
  125.   end
  126.  
  127. /*
  128. if rc ~= 0 then do
  129.   say scriptname": Error" rc "trying to start FTP client '"clientname"'!"
  130.   exit 10
  131.   end
  132. */
  133.  
  134. /*
  135.  * Wait for ARexx port to appear (2 minute timeout).
  136.  */
  137. call time('R')
  138. do forever
  139.   if show('P',clientport) then leave
  140.   call Delay(250)
  141.   if time('E') > 120 then do
  142.     say scriptname": Timeout waiting for client port '"clientport"'!"
  143.     exit 10
  144.     end
  145.   end
  146.  
  147. /*
  148.  * Set up port #.
  149.  */
  150. address value(clientport)
  151. if length(ftpport) > 0 then do
  152.   ""portcmd ftpport portopts
  153.   end
  154.  
  155. /*
  156.  * Get site name & base dir.
  157.  */
  158. address AXFTP
  159. "Site"
  160. site = result
  161. "BaseDir"
  162. basedir = result
  163.  
  164. /*
  165.  * Connect to site.
  166.  */
  167. address value(clientport)
  168. ""sitecmd site siteopts
  169. if length(connectcmd) > 0 then do
  170.   ""connectcmd connectopts
  171.   end
  172.  
  173. if rc ~= "0" then do
  174.   say scriptname": Unable to connect to site '"site"'!"
  175.   address AX "ErrorReq" scriptname" ("rc"): Unable to connect to site '"site"'!"
  176.   address AXFTP "Status" scriptname" ("rc"): Unable to connect to site '"site"'!"
  177.   exit 10
  178.   end
  179.  
  180. /*
  181.  * Set transfer mode.
  182.  */
  183. if length(binarycmd) > 0 then do
  184.   ""binarycmd binaryopts
  185.   end
  186.  
  187. /*
  188.  * Were files or a filelist specified?
  189.  */
  190. filelist = ""
  191. if length(files) > 0 then do
  192.   if upper(substr(files,1,5)) = "LIST=" then do
  193.     filelist = word(substr(files,6),1)
  194.     c = usefilelist(filelist)
  195.     if c ~= 0 then exit c
  196.     signal thatsit
  197.     end
  198.   else do
  199.     c = usefiles(files)
  200.     if c ~= 0 then exit c
  201.     signal thatsit
  202.     end
  203.   end
  204.  
  205. /*
  206.  * The following code controls interaction with Ax (via the ARexx ports AX and
  207.  * AXFTP) and the FTP client to grab files.
  208.  */
  209.  
  210. /* tell Ax to start FTP thread, if not running */
  211. address AX
  212. "StartFTP"
  213. if result = "0" then do
  214.   say scriptname": Unable to start Ax's FTP thread!"
  215.   exit 10
  216.   end
  217. call time('R')
  218. do forever
  219.   if show('P',"AXFTP") then leave
  220.   call Delay(250)
  221.   if time('E') > 120 then do
  222.     say scriptname": Timeout waiting for port 'AXFTP'!"
  223.     exit 10
  224.     end
  225.   end
  226.  
  227. /*
  228.  * Reset file list.
  229.  */
  230. address AXFTP
  231. "ResetFilelist"
  232.  
  233. /*
  234.  * Main file retrieval loop.
  235.  */
  236. do forever
  237.   address AXFTP
  238.   "GetFilename"
  239.   file = result
  240.   if file = "" then leave
  241.   "GetPath ABSOLUTE"
  242.   path = result
  243.  
  244.   /* get file */
  245.   address AXFTP "Status Changing remote directory to '"path"'..."
  246.   address value(clientport) ""cdcmd path cdopts
  247.   address AXFTP "Status Retrieving '"file"'..."
  248.   address value(clientport) ""getcmd file getopts
  249.  
  250.   address AXFTP
  251.   "SetFile DOWNLOADED"
  252.   "RemoveFile"
  253.   "NextFile"
  254.   end
  255.  
  256. thatsit:
  257. /*
  258.  * Tell FTP client to shut down.
  259.  */
  260. if length(quitcmd) > 0 then do
  261.   address value(clientport)
  262.   ""quitcmd quitopts
  263.   end
  264.  
  265. exit 0
  266.  
  267. /*
  268.  * This procedure lets the script grab files via the FTP client using a file
  269.  * list.
  270.  */
  271. usefilelist:
  272.   parse arg flist
  273.  
  274. error = 0
  275.  
  276. if open('fl',flist,'R') then do
  277.   site = strip(readln('fl'))
  278.   basedir = strip(readln('fl'))
  279.   do while ~eof('fl')
  280.     fpath = readln('fl')
  281.     if fpath = "###" then leave
  282.     if substr(fpath,1,1) = "/" then do /* absolute pathname */
  283.       fpath = reverse(fpath)
  284.       parse fpath fname "/" fpath
  285.       fpath = reverse(fpath)
  286.       fname = reverse(fname)
  287.       end
  288.     else if index(fpath,"/") > 0 then do /* relative pathname is specified */
  289.       fpath = reverse(basedir"/"fpath)
  290.       parse fpath fname "/" fpath
  291.       fpath = reverse(fpath)
  292.       fname = reverse(fname)
  293.       end
  294.     else do
  295.       fname = fpath
  296.       fpath = basedir
  297.       end
  298.     address value(clientport)
  299.     ""cdcmd fpath cdopts
  300.     ""getcmd fname getopts
  301.     end
  302.   call close('fl')
  303.   end
  304.  
  305. return error
  306.  
  307. /*
  308.  * This procedure lets the script grab files via the FTP client using files
  309.  * specified on the command line.
  310.  */
  311. usefiles:
  312.   parse arg site basedir flist
  313.  
  314. error = 0
  315.  
  316. do forever
  317.   parse var flist fpath flist
  318.   if fpath = "" then leave
  319.   if substr(fpath,1,1) = "/" then do /* absolute pathname */
  320.     fpath = reverse(fpath)
  321.     parse fpath fname "/" fpath
  322.     fpath = reverse(fpath)
  323.     fname = reverse(fname)
  324.     end
  325.   else if index(fpath,"/") > 0 then do /* relative pathname is specified */
  326.     fpath = reverse(basedir"/"fpath)
  327.     parse fpath fname "/" fpath
  328.     fpath = reverse(fpath)
  329.     fname = reverse(fname)
  330.     end
  331.   else do
  332.     fname = fpath
  333.     fpath = basedir
  334.     end
  335.   end
  336.   address value(clientport)
  337.   ""cdcmd fpath cdopts
  338.   ""getcmd fname getopts
  339.   end
  340.  
  341. return error
  342.  
  343. syntax:
  344. err = rc
  345. parse source . . . me .
  346. address AX "ErrorReq ARexx error" err "in line" sigl "of" me"."
  347. exit
  348.  
  349. error:
  350. parse source . . . me .
  351. address AX "ErrorReq Error" rc "in line" sigl "of" me"."
  352.